<?php

/**
 * @author      :   phongtx@fpt.net
 * @name        :   PhotoController
 * @copyright   :   Fpt Online
 * @todo        :   Photo Controller
 */
class PhotoController extends Zend_Controller_Action {

    /**
     * init of controller
     */
    public function init() {
        
    }

    /**
     * Default action
     */
    public function indexAction() {
        //Get album noi bat
        $newsModel = News::getInstance();

        $arrHotAlbum = $newsModel->getListTopArticle(CATE_ID_PHOTO, LIMIT_HOT_ALBUM, 0, ALBUM);
        $arrHotAlbum = $arrHotAlbum['data'];
      
        //Get cate
        $cateModel = Category::getInstance();
        $arrCateMenu = $cateModel->getMenu(CATE_ID_PHOTO);
        //Lay tat ca cac album cua menu
        $arrListAlbum = $newsModel->getListTopArticle(CATE_ID_PHOTO, LIMIT_NEW_PHOTO_SITE + LIMIT_HOT_ALBUM, 0, ALBUM);  
        $arrListAlbum = (array)$arrListAlbum['data'];       
    
        $countHot = count($arrHotAlbum);       
        if ($countHot == 0) {
            $arrHotAlbum = array_slice($arrListAlbum, 0, LIMIT_HOT_ALBUM);
            $arrListAlbum = array_slice($arrListAlbum, LIMIT_HOT_ALBUM, LIMIT_NEW_PHOTO_SITE);
        } else {
            $arrListAlbum = array_diff($arrListAlbum, $arrHotAlbum);
            
            if ($countHot < LIMIT_HOT_ALBUM) {
               
                $arrHotAlbum = array_merge($arrHotAlbum, array_slice($arrListAlbum, 0,(LIMIT_HOT_ALBUM-$countHot)));
                
            }
            $arrListAlbum = array_slice($arrListAlbum, (LIMIT_HOT_ALBUM - $countHot), LIMIT_NEW_PHOTO_SITE);
        }
        $arrExcluse = array();
        foreach ($arrListAlbum as $key => $value)
        {
            array_push($arrExcluse,(int)$value);
        }
        $arrDetail['hotalbum'] = $newsModel->getDetailArticleByIDs($arrHotAlbum);
        $arrDetail['album'] = $newsModel->getDetailArticleByIDs($arrListAlbum);
        $this->view->total = count($arrDetail['album']) >= LIMIT_NEW_PHOTO_SITE ? count($arrDetail['album']) + 1 : 0;
        //Assign data to view
        $this->view->arrCateMenu = $arrCateMenu;
        $this->view->arrDetail = $arrDetail;
        $this->view->arrExcluse = Zend_Json::encode($arrExcluse);
        // assign to view
        //$this->view->arrNewPhotoResult = $arrNewPhotoResult["data"];
        //$this->view->total = $arrNewPhotoResult["total"];
        $this->view->cateID = CATE_ID_PHOTO;
        /* END */

        $this->view->headTitle('Lưu trữ hình ảnh cầu thủ, trận đấu các giải bóng đá trong nước và thế giới - VnExpress');
        $this->view->headMeta()->setName('description', 'Lưu trữ hình ảnh của các cầu thủ, pha ghi bàn, bàn thắng đẹp, sự kiện các trận đấu của các giải bóng đá trong nước và quốc tế.');
        $this->view->headMeta()->setName('keywords', 'Hinh anh,bong da,the thao');
        $this->view->categoryID = CATE_ID_PHOTO;
    }
    public function detailAction()
    {
        $catecode    = $this->_request->getParam('catecode');
        $title = $this->_request->getParam('title');
        $albumId    = intval($this->_request->getParam('albumid'));
        if($albumId > 0)
        {
            //init model cate
            $cateModel = Category::getInstance();
            $arrCateMenu = $cateModel->getMenu(CATE_ID_PHOTO);
            
            //init model news
            $newsModel = News::getInstance();
            
            //init page
            $intPage = $this->_request->getParam('page',1);

            //Set start offset
            $perpage = LIMIT_ALBUM;

            //Get Start Page
            $intStart = ($intPage - 1) * $perpage;
            
            //get list album
            $arrAlbums = $newsModel->getDetailArticleByIDs(array($albumId));
            //Get list photo
            $arrPhotos = $newsModel->getObjectReferenceByArticleID($albumId, LIMIT_PHOTO, 0);
            //get cateId
            $arrCateId =  $arrAlbums[$albumId]['category_id'];
            //get list album by cateId
            $arrArticleIds = $newsModel->getListTopArticle($arrCateId,$perpage,$intStart, ALBUM);
            
           
            $arrResult = array_diff($arrArticleIds['data'],array($albumId));
            $intTotal = 0;
            $arrAlbumByArticleIds = array();
            if(!empty ($arrResult)&& count($arrResult))
            {
                $arrAlbumByArticleIds = $newsModel->getDetailArticleByIDs($arrResult);
            }
            $intTotal = $arrArticleIds['total']-1;
            $strUri = '/photo/'.$catecode.'/'.$title.'-'.$albumId;
            //InitParam Paging
            $arrParamPaging = array(
                'total'         =>  $intTotal,
                'page'          =>  $intPage,
                'uri'           =>  $strUri,
                'showItem'      =>  3,
                'perpage'       =>  $perpage,
                'idPagination'  =>  'pagination',
                'extEnd'        =>  '.html',
                'separate'      =>  '-'
            );
            //Assign to view
            $this->view->arrCateMenu = $arrCateMenu;
            $this->view->cateID = $arrCateId;
            $this->view->catecode = $catecode;
            $this->view->arrAlbums = $arrAlbums;
            $this->view->arrPhotos = $arrPhotos;
            $this->view->arrAlbumByArticleIds = $arrAlbumByArticleIds;
            $this->view->total = $intTotal;
            $this->view->arrParamPaging = $arrParamPaging;
        }
    }

}